package com.handpay.nocardpay.baselibrary.commbase.view.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.NoCardPayApplication;
import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.account.view.activity.LoginActivity;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseErrorInfo;
import com.handpay.nocardpay.baselibrary.commbase.view.IBaseView;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.notification.service.NoticeService;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;
import com.handpay.nocardpay.baselibrary.utils.StatusBarUtil;
import com.umeng.analytics.MobclickAgent;

import me.yokeyword.fragmentation.SupportFragment;
import me.yokeyword.fragmentation.anim.DefaultNoAnimator;

/**
 * Created by sxshi on 2017/8/8.
 */

public abstract class BaseFragment extends SupportFragment implements IBaseView {

    // 再点一次退出程序时间设置
    public static final long WAIT_TIME = 2000L;
    public long TOUCH_TIME = 0;

    public abstract int getLayoutId();

    public abstract void initView(View view);

    public abstract void initListener();

    public abstract void initData(Bundle savedInstanceState);

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mRootView = inflater.inflate(getLayoutId(), container, false);
        setFragmentAnimator(new DefaultNoAnimator());
        initView(mRootView);
        initListener();
        initData(savedInstanceState);
        setStatusBar(null);
        return mRootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(_mActivity);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(_mActivity);
    }

    @Override
    public void showProgress() {
        DialogUtils.showProgressDialog(getActivity(), "", _mActivity.getString(R.string.loading), false);
    }

    @Override
    public void hideProgress() {
        DialogUtils.closeProgressDialog();
    }

    /**
     * 基类统一处理错误信息提示，<br/>
     * 如果子类页面需要特殊处理则实现此方法修改<br/>
     * sample {@link com.handpay.nocardpay.baselibrary.account.view.fragment.NoCardLoginFragment#showErrorAlert(BaseErrorInfo)}
     *
     * @param baseErrorInfo 错误信息
     */
    @Override
    public void showErrorAlert(BaseErrorInfo baseErrorInfo) {
        if (baseErrorInfo.responseCode.equals("99") || baseErrorInfo.responseCode.equals("999")) {
            //2017/11/15 在所有接口返回99的情况提示退出到登入页面（首付通暂时没有999的情况所以先不处理）
            DialogUtils.showAlertDialog(_mActivity, _mActivity.getString(R.string.dialog_title), baseErrorInfo.errMessage, false, _mActivity.getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    _mActivity.stopService(new Intent(_mActivity, NoticeService.class));
                    Intent intent = new Intent();
                    intent.setClass(_mActivity, LoginActivity.class);
                    intent.putExtra(BaseProtocolKey.LOGIN_TYPE, NoCardPayConfig.isNoCardPay);
                    _mActivity.startActivity(intent);
                    _mActivity.finish();
//                    NoCardPayApplication.getApp().exitAppaction();
                }
            });
        } else {
            DialogUtils.showAlertDialog(_mActivity, _mActivity.getString(R.string.dialog_title), baseErrorInfo.errMessage, false);
        }
    }

    /***
     * 初始化ToolBar
     *
     * @param toolbar
     * @param leftBackIcon 是否显示左边返回按钮
     * @param rightIcon    是否显示右边设置按钮
     * @param listener     右边按钮监听事件
     */
    protected void initToolbar(Toolbar toolbar, boolean leftBackIcon, boolean rightIcon, View.OnClickListener listener) {
        TextView mTitle = (TextView) toolbar.findViewById(R.id.tv_title);
        ImageView mRightView = (ImageView) toolbar.findViewById(R.id.iv_right);
        toolbar.setTitle("");
        if (null != getFragmentTitle()) {
            mTitle.setText(getFragmentTitle());
        } else {
            mTitle.setText("");//未设置Fragement title
        }
        if (leftBackIcon) {
            toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    hideSoftInput();
                    pop();
                }
            });
        }
        if (rightIcon) {
            mRightView.setVisibility(View.VISIBLE);
            mRightView.setOnClickListener(listener);
        }
    }

    /***
     * 初始化ToolBar 默认左右两边按钮都显示
     *
     * @param toolbar
     * @param leftListener 左边按钮监听事件
     * @param rightListener 右边按钮监听事件
     */
    protected void initToolbar(Toolbar toolbar, View.OnClickListener leftListener, View.OnClickListener rightListener) {
        TextView mTitle = (TextView) toolbar.findViewById(R.id.tv_title);
        ImageView mRightView = (ImageView) toolbar.findViewById(R.id.iv_right);
        toolbar.setTitle("");
        if (null != getFragmentTitle()) {
            mTitle.setText(getFragmentTitle());
        } else {
            mTitle.setText("");//未设置Fragement title
        }
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);

        if (leftListener != null) {
            toolbar.setNavigationOnClickListener(leftListener);
        } else {
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    hideSoftInput();
                    pop();
                }
            });
        }

        mRightView.setVisibility(View.VISIBLE);
        if (rightListener != null)
            mRightView.setOnClickListener(rightListener);
    }

    /***
     * 初始化 ToolBar
     *
     * @param toolbar
     * @param leftBackIcon 左边返回按钮是否显示
     */
    protected void initToolbar(Toolbar toolbar, boolean leftBackIcon) {
        TextView mTitle = (TextView) toolbar.findViewById(R.id.tv_title);
        ImageView mRightView = (ImageView) toolbar.findViewById(R.id.iv_right);
        toolbar.setTitle("");
        if (null != getFragmentTitle()) {
            mTitle.setText(getFragmentTitle());
        } else {
            mTitle.setText("");//未设置Fragement title
        }
        if (leftBackIcon) {
            toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    hideSoftInput();
                    pop();
                }
            });
        }
    }

    protected void initToolbar(Toolbar toolbar, boolean leftBackIcon, View.OnClickListener leftListener) {
        TextView mTitle = (TextView) toolbar.findViewById(R.id.tv_title);
        ImageView mRightView = (ImageView) toolbar.findViewById(R.id.iv_right);
        toolbar.setTitle("");
        if (null != getFragmentTitle()) {
            mTitle.setText(getFragmentTitle());
        } else {
            mTitle.setText("");//未设置Fragement title
        }
        if (leftBackIcon) {
            toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
            toolbar.setNavigationOnClickListener(leftListener);
        }
    }

    /***
     * @return 返回Fragmnet title
     */
    public String getFragmentTitle() {
        return null;
    }

    /***
     * 设置状态栏的颜色，可以自行重写此方法
     */
    public void setStatusBar(View view) {
        StatusBarUtil.setColor(_mActivity, _mActivity.getResources().getColor(R.color.colorPrimary), 0);
//        StatusBarUtil.setTranslucent(_mActivity);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        hideSoftInput();
    }
}
